package com.dlc.shop.systemtest.api.amount;


import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import com.dlc.shop.bean.app.dto.OrderNumbersDto;
import com.dlc.shop.bean.app.dto.ShopCartOrderMergerDto;
import com.dlc.shop.bean.model.OfflineHandleEvent;
import com.dlc.shop.bean.param.OfflineHandleEventAuditParam;
import com.dlc.shop.common.util.Json;
import com.dlc.shop.groupbuy.common.api.param.ApiGroupOrderSubmitParam;
import com.dlc.shop.groupbuy.common.api.param.GroupOrderParam;
import com.dlc.shop.groupbuy.common.api.vo.ApiGroupProdVo;
import com.dlc.shop.groupbuy.common.model.GroupActivity;
import com.dlc.shop.systemtest.constants.Constant;
import com.dlc.shop.systemtest.util.CheckUtil;
import com.dlc.shop.systemtest.util.HttpUtil;

/**
 * @author lh
 */
public class GroupApi {

    public static void groupInfo(GroupActivity groupActivity){
        String url = Constant.SHOP_URL+"/info/{groupActivityId}";
        HttpUtil.get(url);
    }

    public static void createGroup(GroupActivity groupActivity){
        String url = Constant.SHOP_URL+"/group/activity";
        HttpUtil.post(url,groupActivity);
    }

    public static void updateGroup(GroupActivity groupActivity){
        String url = Constant.SHOP_URL+"/group/activity";
        HttpUtil.put(url,groupActivity);
    }

    /**
     * 新增拼团活动
     */
    public static Long addGroup(GroupActivity groupActivity) {
        String url = Constant.SHOP_URL + "/group/activity";
        return HttpUtil.post(url, groupActivity, Long.class);
    }

    /**
     * 根据活动id查询拼团活动
     */
    public static GroupActivity getGroup(Long groupActivityId) {
        String url = Constant.SHOP_URL + "/group/activity/info/" + groupActivityId;
        return HttpUtil.get(url, GroupActivity.class);
    }

    /**
     * 根据活动id启用拼团活动
     */
    public static void enableGroup(Long groupActivityId) {
        String url = Constant.SHOP_URL + "/group/activity/active/" + groupActivityId;
        HttpUtil.put(url);
    }

    /**
     * 根据活动ID失效活动
     */
    public static void disableGroup(Long groupActivityId) {
        String url = Constant.SHOP_URL + "/group/activity/invalid/" + groupActivityId;
        HttpUtil.put(url);
    }

    /**
     * 根据活动ID删除活动
     */
    public static void deleteGroup(Long groupActivityId) {
        String url = Constant.SHOP_URL + "/group/activity/" + groupActivityId;
        HttpUtil.delete(url);
    }

    /**
     * 平台违规下架拼团活动
     */
    public static void offlineGroup(OfflineHandleEvent offlineHandleEvent) {
        String url = Constant.PLATFORM_URL + "/platform/group/activity/offline";
        HttpUtil.post(url, offlineHandleEvent);
    }

    /**
     * 商家根据活动ID获取下架信息
     */
    public static OfflineHandleEvent getOfflineHandleEvent(Long groupActivityId) {
        String url = Constant.SHOP_URL + "/group/activity/getOfflineHandleEventByActivityId/" + groupActivityId;
        return HttpUtil.get(url, OfflineHandleEvent.class);
    }

    /**
     * 商家根据活动下架信息申请上架
     */
    public static void applyOnline(OfflineHandleEventAuditParam offlineHandleEventAuditParam) {
        String url = Constant.SHOP_URL + "/group/activity/auditApply";
        HttpUtil.post(url, offlineHandleEventAuditParam);
    }

    /**
     * 平台根据活动ID获取下架信息
     */
    public static OfflineHandleEvent platformGetOfflineHandleEvent(Long groupActivityId) {
        String url = Constant.PLATFORM_URL + "/platform/group/activity/getOfflineHandleEventByActivityId/" + groupActivityId;
        return HttpUtil.get(url, OfflineHandleEvent.class);
    }

    /**
     * 平台根据活动下架信息审核
     */
    public static void audit(OfflineHandleEventAuditParam offlineHandleEventAuditParam) {
        String url = Constant.PLATFORM_URL + "/platform/group/activity/auditGroupActivity";
        HttpUtil.post(url, offlineHandleEventAuditParam);
    }

    /**
     * 用户开团
     */
    public static void openGroup(GroupOrderParam groupOrderParam) {
        String url = Constant.API_URL + "/p/group/order/confirm";
        HttpUtil.post(url, groupOrderParam);
    }

    /**
     * 用户获取可参团列表
     */
    public static ApiGroupProdVo getJoinGroupList(Long groupActivityId) {
        String url = Constant.API_URL + "/group/joinGroupList?groupActivityId=" + groupActivityId + "&showSize=99";
        return HttpUtil.get(url, ApiGroupProdVo.class);
    }


    /**
     * 提交团购订单
     */
    public static OrderNumbersDto submitGroupOrder(ApiGroupOrderSubmitParam apiGroupOrderSubmitParam) {
        String url = Constant.API_URL + "/p/group/order/submit";
        return HttpUtil.post(url, apiGroupOrderSubmitParam, OrderNumbersDto.class);
    }

    /**
     * 用户参团
     */
    public static void joinGroup(GroupOrderParam groupOrderParam) {
        String url = Constant.API_URL + "/p/group/order/confirm";
        HttpRequest post = HttpRequest.post(url);
        post.header("authorization", Constant.OTHER_API_TOKEN);
        post.header("Content-type", "application/json; charset=UTF-8");
        HttpResponse response = post.body(Json.toJsonString(groupOrderParam)).execute();
        CheckUtil.checkAndGetObject(response, ShopCartOrderMergerDto.class);
    }

    /**
     * 参团用户提交团购订单
     */
    public static OrderNumbersDto submitJoinGroupOrder(ApiGroupOrderSubmitParam apiGroupOrderSubmitParam) {
        String url = Constant.API_URL + "/p/group/order/submit";
        HttpRequest post = HttpRequest.post(url);
        post.header("authorization", Constant.OTHER_API_TOKEN);
        post.header("Content-type", "application/json; charset=UTF-8");
        HttpResponse response = post.body(Json.toJsonString(apiGroupOrderSubmitParam)).execute();
        return CheckUtil.checkAndGetObject(response, OrderNumbersDto.class);
    }


}
